#include <thread>
#include <chrono>

#include "metrics/utility/CPUMetricsBuilder.hpp"
#include "metrics/utility/MemMetricsBuilder.hpp"
#include "metrics/Counter.hpp"
#include "metrics/Builder.hpp"
#include "metrics/KafkaMetricReporter.hpp"

int main(int argc, char const *argv[])
{
    //1. 构建reporter
    std::string brokers("192.168.37.204:9092");
    std::string topic("dataflow_metrics");
    metrics::KafkaMetricReporter reporter(brokers, topic);
    //2. 构建metrics registry
    auto registry = std::make_shared<metrics::MetricRegistry>();

    //3. 设置metric通用标签, 可以设置appId, host等有用信息
    std::map<std::string, std::string> m{{"a", "b"}, {"c", "d"}, {"appId", "ro-1"}};

    //4. 构建metric family
    std::shared_ptr<metrics::Family<metrics::Counter>> counterFamily = metrics::BuildCounter().name("count_total").help("test").labels(m).metric_register(registry);
    //5. 添加metric, 使用不同的labels来区分同一个family中的不同metric
    std::shared_ptr<metrics::Counter> inputCount = counterFamily->add({{"direct", "input"}});
    std::shared_ptr<metrics::Counter> outputCount = counterFamily->add({{"direct", "output"}});
    //====== 添加cpu mem metric, 不需要用户更新cpu和mem, 仅需注册=======
    metrics::BuildCPUMetrics().labels(m).metric_register(registry);
    metrics::BuildMemMetrics().labels(m).metric_register(registry);
    //6. 将metrics registry提交到reporter, 并设置report时间间隔
    reporter.register_collectable(registry).interval(1, 0);

    std::thread th([&reporter]()
                   { reporter.start(); });

    for (size_t i = 0; i < 10; i++)
    {
        inputCount->increment(33);
        outputCount->increment(100);
        std::this_thread::sleep_for(std::chrono::seconds(1));
    }

    reporter.stop();
    th.join();
    return 0;
}
